home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / newusr32.fr_ / newusr32.fr
Text File  |  1995-09-04  |  6KB  |  200 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Add User"
  5.    ClientHeight    =   2760
  6.    ClientLeft      =   1080
  7.    ClientTop       =   1515
  8.    ClientWidth     =   4980
  9.    BeginProperty Font 
  10.       name            =   "MS Sans Serif"
  11.       charset         =   0
  12.       weight          =   700
  13.       size            =   8.25
  14.       underline       =   0   'False
  15.       italic          =   0   'False
  16.       strikethrough   =   0   'False
  17.    EndProperty
  18.    Height          =   3165
  19.    Left            =   1020
  20.    LinkTopic       =   "Form1"
  21.    ScaleHeight     =   2760
  22.    ScaleWidth      =   4980
  23.    Top             =   1170
  24.    Width           =   5100
  25.    Begin VB.TextBox txtUserName 
  26.       Height          =   315
  27.       Left            =   2160
  28.       TabIndex        =   7
  29.       Top             =   360
  30.       Width           =   2115
  31.    End
  32.    Begin VB.CommandButton cmdClose 
  33.       Cancel          =   -1  'True
  34.       Caption         =   "Cl&ose"
  35.       Height          =   555
  36.       Left            =   2520
  37.       TabIndex        =   6
  38.       Top             =   1920
  39.       Width           =   1755
  40.    End
  41.    Begin VB.CommandButton cmdAddUser 
  42.       Caption         =   "&Add User"
  43.       Default         =   -1  'True
  44.       Height          =   555
  45.       Left            =   480
  46.       TabIndex        =   5
  47.       Top             =   1920
  48.       Width           =   1755
  49.    End
  50.    Begin VB.TextBox txtVerify 
  51.       Height          =   285
  52.       Left            =   2160
  53.       TabIndex        =   4
  54.       Top             =   1320
  55.       Width           =   2115
  56.    End
  57.    Begin VB.TextBox txtPassword 
  58.       Height          =   285
  59.       Left            =   2160
  60.       TabIndex        =   3
  61.       Top             =   840
  62.       Width           =   2115
  63.    End
  64.    Begin VB.Label Label4 
  65.       Alignment       =   1  'Right Justify
  66.       AutoSize        =   -1  'True
  67.       BackColor       =   &H00C0C0C0&
  68.       Caption         =   "&Retype to verify:"
  69.       Height          =   195
  70.       Left            =   540
  71.       TabIndex        =   2
  72.       Top             =   1380
  73.       Width           =   1425
  74.    End
  75.    Begin VB.Label Label3 
  76.       Alignment       =   1  'Right Justify
  77.       AutoSize        =   -1  'True
  78.       BackColor       =   &H00C0C0C0&
  79.       Caption         =   "&Password:"
  80.       Height          =   195
  81.       Left            =   1050
  82.       TabIndex        =   1
  83.       Top             =   900
  84.       Width           =   885
  85.    End
  86.    Begin VB.Label Label1 
  87.       Alignment       =   1  'Right Justify
  88.       AutoSize        =   -1  'True
  89.       BackColor       =   &H00C0C0C0&
  90.       Caption         =   "&User name:"
  91.       Height          =   195
  92.       Left            =   900
  93.       TabIndex        =   0
  94.       Top             =   420
  95.       Width           =   975
  96.    End
  97. End
  98. Attribute VB_Name = "Form1"
  99. Attribute VB_Creatable = False
  100. Attribute VB_Exposed = False
  101. Option Explicit
  102.  
  103. Private Declare Function GetPrivateProfileString _
  104.     Lib "Kernel32" Alias "GetPrivateProfileStringA" (ByVal lpSectionName As String, _
  105.     ByVal lpKeyName As Any, ByVal lpDefault As String, _
  106.     ByVal lpReturnedString As String, ByVal Size As Integer, _
  107.     ByVal lpFileName As String) As Integer
  108.  
  109.  
  110. Private Sub Form_Load()
  111.     Dim myUser As String, myPass As String
  112.     Dim winDir As String * 128
  113.     Dim dirLen As Integer
  114.     
  115.     On Error GoTo LoadError
  116.     
  117.     ' Set the user and passwords for initial login.
  118.     myUser = "Admin"
  119.     myPass = "theboss"
  120.     
  121.     ' read VBDBHT.INI to get the name of the system database,
  122.     ' then assign that name to the SystemDB property
  123.     DBEngine.SystemDB = GetSystemDatabase()
  124.  
  125.     ' log in
  126.     DBEngine.DefaultUser = myUser
  127.     DBEngine.DefaultPassword = myPass
  128.  
  129. Exit Sub
  130. LoadError:
  131.     MsgBox Err & " " & Error$
  132. End
  133.  
  134. End Sub
  135.  
  136.  
  137. Private Sub cmdAddUser_Click()
  138.     Dim newUser As User
  139.     Dim thePID As String
  140.     
  141.     On Error GoTo ChangeError
  142.     
  143.     If txtUserName = "" Then Error 32765
  144.     If txtPassword <> "" And txtPassword <> txtVerify Then Error 32767
  145.     If Len(txtPassword) > 14 Then Error 32766
  146.     thePID = txtUserName
  147.     If Len(thePID) > 20 Then
  148.         thePID = Left$(thePID, 20)
  149.     Else
  150.         Do While Len(thePID) < 4
  151.             thePID = thePID & "_"
  152.         Loop
  153.     End If
  154.     Set newUser = DBEngine.Workspaces(0).CreateUser(txtUserName, thePID, txtPassword)
  155.     DBEngine.Workspaces(0).Users.Append newUser
  156.     MsgBox "User " & txtUserName & " created", vbInformation
  157.     txtUserName = ""
  158.     txtPassword = ""
  159.     txtVerify = ""
  160. Exit Sub
  161. ChangeError:
  162.     Dim msg As String
  163.     Select Case Err.Number
  164.         Case 3390
  165.             msg = "There is already a user with that name in the system database"
  166.         Case 32765
  167.             msg = "You have not selected a user"
  168.         Case 32766
  169.             msg = "The password may not be longer than 14 characters"
  170.             txtPassword = ""
  171.             txtVerify = ""
  172.         Case 32767
  173.             msg = "The verify box does not match the password box"
  174.             txtPassword = ""
  175.             txtVerify = ""
  176.         Case Else
  177.             msg = Err.Description & " (" & Err.Number & ")"
  178.     End Select
  179.     MsgBox msg, vbExclamation
  180. End Sub
  181.  
  182. Private Sub cmdClose_Click()
  183.     End
  184. End Sub
  185.  
  186. Private Function GetSystemDatabase() As String
  187.     ' Returns the name of the system directory
  188.     
  189.     Const INI_FILENAME = "VBDBHT.INI"
  190.     Const MAX_PATH = 128
  191.  
  192.     Dim lpReturnedString As String * MAX_PATH
  193.     Dim bytesBack As Integer
  194.     
  195.     bytesBack = GetPrivateProfileString("Options", _
  196.         "SystemDB", "", lpReturnedString, MAX_PATH, INI_FILENAME)
  197.     GetSystemDatabase = IIf(bytesBack > 0, Left$(lpReturnedString, bytesBack), "")
  198.     
  199. End Function
  200.